package com.encryption.logic.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.dao.FileDAO;
import com.encryption.dao.FilePermissionAssoDAO;
import com.encryption.dao.UserDAO;
import com.encryption.logic.ActionPermission;
import com.encryption.logic.UserConstant;
import com.encryption.model.File;
import com.encryption.model.FilePermissionAsso;
import com.encryption.model.User;

/**
 * 文件动作权限接口实现类
 * 
 * @author Administrator
 * 
 */
public class ActionPermissionImpl implements ActionPermission {

	private static final Logger log = LoggerFactory
			.getLogger(ActionPermissionImpl.class);

	private static ActionPermissionImpl actionPermissionImpl = null;

	public static ActionPermissionImpl getActionPermissionImpl() {
		if (null == actionPermissionImpl) {
			actionPermissionImpl = new ActionPermissionImpl();
		}
		return actionPermissionImpl;
	}

	/**
	 * 判断用户是否有权限对该文件操作
	 * 
	 * @param user
	 * @param file
	 * @return
	 */
	public boolean hasPermission(User user, File file) {
		return false;
	}

	/**
	 * 动作类型
	 * 
	 * @return
	 */
	public String getActionType() {
		return null;
	}

	/**
	 * 判断是否有删除权限 只有上传者才能够删除
	 */
	public boolean hasDeleteFilePermission(User user, File file) {
		File getFile = FileDAO.getFileDAO().findById(file.getId());
		if (user.getId() == getFile.getUser()) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否有下载权限 允许访问的类型（个人还是部门2、仅自己1、全部0） 如果,根据用户能查询出该用户有下载权限则返回.
	 * 否则,再查看该用户所在的部门是否有该文件的下载权限.
	 * 
	 * @return
	 */
	public boolean hasDownloadFilePermission(User user, File file) {

		// 1.查询个人对文件是否有下载权限, allowType 为1,仅自己
		List<FilePermissionAsso> filePermissionAssoList = FilePermissionAssoDAO
				.getFilePermissionAssoDAO().hasPermission(file.getId(),
						user.getId(), UserConstant.USERALLOWTYPE);

		if (null != filePermissionAssoList
				&& 0 != filePermissionAssoList.size()) {
			for (FilePermissionAsso filePermissionAsso : filePermissionAssoList) {
				if (UserConstant.ALLOW == filePermissionAsso.getAllowDownload()) {
					return true;
				}
			}
		}

		// 2.查询个人所在的部门对文件是否有下载权限, allowType 为2,所在部门
		// A.根据用户id,查询该用户所在的部门id.
		Integer departmentId = UserDAO.getUserDAO().findById(user.getId())
				.getDepartment();

		// B.查询该用户所在部门是否有该文档的下载权限
		List<FilePermissionAsso> filePermissionAssoByDeptList = FilePermissionAssoDAO
				.getFilePermissionAssoDAO().hasPermission(file.getId(),
						departmentId, UserConstant.DEPTALLOWTYPE);

		if (null != filePermissionAssoByDeptList
				&& 0 != filePermissionAssoByDeptList.size()) {
			for (FilePermissionAsso filePermissionAsso : filePermissionAssoByDeptList) {
				if (UserConstant.ALLOW == filePermissionAsso.getAllowDownload()) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断是否有查看权限
	 * 
	 * @return
	 */
	public boolean hasViewFilePermission(User user, File file) {
		// 1.查询个人对文件是否有查看权限, allowType 为1,仅自己
		List<FilePermissionAsso> filePermissionAssoList = FilePermissionAssoDAO
				.getFilePermissionAssoDAO().hasPermission(file.getId(),
						user.getId(), UserConstant.USERALLOWTYPE);

		if (null != filePermissionAssoList
				&& 0 != filePermissionAssoList.size()) {
			for (FilePermissionAsso filePermissionAsso : filePermissionAssoList) {
				if (UserConstant.ALLOW == filePermissionAsso.getAllowView()) {
					return true;
				}
			}
		}

		// 2.查询个人所在的部门对文件是否有查看权限, allowType 为2,所在部门
		// A.根据用户id,查询该用户所在的部门id.
		Integer departmentId = UserDAO.getUserDAO().findById(user.getId())
				.getDepartment();

		// B.查询该用户所在部门是否有该文档的查看权限
		List<FilePermissionAsso> filePermissionAssoByDeptList = FilePermissionAssoDAO
				.getFilePermissionAssoDAO().hasPermission(file.getId(),
						departmentId, UserConstant.DEPTALLOWTYPE);

		if (null != filePermissionAssoByDeptList
				&& 0 != filePermissionAssoByDeptList.size()) {
			for (FilePermissionAsso filePermissionAsso : filePermissionAssoByDeptList) {
				if (UserConstant.ALLOW == filePermissionAsso.getAllowView()) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断是否有打印权限
	 * 
	 * @return
	 */
	public boolean hasPrintFilePermission(User user, File file) {
		// 1.查询个人对文件是否有打印权限, allowType 为1,仅自己
		List<FilePermissionAsso> filePermissionAssoList = FilePermissionAssoDAO
				.getFilePermissionAssoDAO().hasPermission(file.getId(),
						user.getId(), UserConstant.USERALLOWTYPE);

		if (null != filePermissionAssoList
				&& 0 != filePermissionAssoList.size()) {
			for (FilePermissionAsso filePermissionAsso : filePermissionAssoList) {
				if (UserConstant.ALLOW == filePermissionAsso.getAllowPrint()) {
					return true;
				}
			}
		}

		// 2.查询个人所在的部门对文件是否有打印权限, allowType 为2,所在部门
		// A.根据用户id,查询该用户所在的部门id.
		Integer departmentId = UserDAO.getUserDAO().findById(user.getId())
				.getDepartment();

		// B.查询该用户所在部门是否有该文档的打印权限
		List<FilePermissionAsso> filePermissionAssoByDeptList = FilePermissionAssoDAO
				.getFilePermissionAssoDAO().hasPermission(file.getId(),
						departmentId, UserConstant.DEPTALLOWTYPE);

		if (null != filePermissionAssoByDeptList
				&& 0 != filePermissionAssoByDeptList.size()) {
			for (FilePermissionAsso filePermissionAsso : filePermissionAssoByDeptList) {
				if (UserConstant.ALLOW == filePermissionAsso.getAllowPrint()) {
					return true;
				}
			}
		}
		return false;
	}
}
